package org.jetlinks.community.mstn.entity;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import org.hswebframework.ezorm.rdb.mapping.annotation.ColumnType;
import org.hswebframework.ezorm.rdb.mapping.annotation.Comment;
import org.hswebframework.ezorm.rdb.mapping.annotation.DefaultValue;
import org.hswebframework.ezorm.rdb.mapping.annotation.JsonCodec;
import org.hswebframework.web.api.crud.entity.GenericEntity;
import org.hswebframework.web.api.crud.entity.RecordCreationEntity;
import org.hswebframework.web.api.crud.entity.RecordModifierEntity;
import org.hswebframework.web.crud.generator.Generators;
import org.hswebframework.web.validator.CreateGroup;

import javax.persistence.Column;
import javax.persistence.Index;
import javax.persistence.Table;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.sql.JDBCType;
import java.util.Map;

@Getter
@Setter
@Table(name = "m_custom_form", indexes = {
    @Index(name = "idx_form_state", columnList = "state"),
    @Index(name = "idx_form_type", columnList = "type")

})
public class CustomFormEntity extends GenericEntity<String> implements RecordCreationEntity, RecordModifierEntity {

    @Comment("表单名称")
    @Column(name = "name")
    @NotBlank(message = "表单名称不能为空", groups = CreateGroup.class)
    @Schema(description = "表单名称")
    private String name;

    @Comment("说明")
    @Column(name = "describe")
    @Schema(description = "说明")
    private String describe;

    @Comment("表单类型")
    @Column(name = "type", length = 32)
    @NotBlank(message = "表单类型不能为空", groups = CreateGroup.class)
    @Schema(description = "表单类型")
    private String type;

    @Comment("配置")
    @Column(name = "configuration")
    @ColumnType(jdbcType = JDBCType.CLOB)
    @JsonCodec
    @Schema(description = "信息")
    private Map<String, Object> configuration;

    @Column(name = "state",nullable = false)
    @Comment("状态")
    @Schema(description = "状态,0禁用,1启用")
    @DefaultValue("1")
    private @NotNull(message = "状态不能为空",groups = {CreateGroup.class}
    ) Byte state;

    @Column(name = "creator_id", updatable = false)
    @Schema(
        description = "创建者ID(只读)"
        ,accessMode = Schema.AccessMode.READ_ONLY
    )
    private String creatorId;

    @Column(name = "creator_name", updatable = false)
    @Schema(
        description = "创建者名称(只读)"
         ,accessMode = Schema.AccessMode.READ_ONLY
    )
    private String creatorName;

    @Column(name = "create_time", updatable = false)
    @DefaultValue(generator = Generators.CURRENT_TIME)
    @Schema(
        description = "创建时间(只读)"
        , accessMode = Schema.AccessMode.READ_ONLY
    )
    private Long createTime;

    @Column(name = "modifier_id", updatable = false)
    @Schema(
        description = "修改者ID(只读)"
        ,accessMode = Schema.AccessMode.READ_ONLY
    )
    private String modifierId;

    @Column(name = "modify_name", updatable = false)
    @Schema(
        description = "修改者名称(只读)"
         ,accessMode = Schema.AccessMode.READ_ONLY
    )
    private String modifyName;

    @Column(name = "modify_time", updatable = false)
    @DefaultValue(generator = Generators.CURRENT_TIME)
    @Schema(
        description = "更新时间(只读)"
        , accessMode = Schema.AccessMode.READ_ONLY
    )
    private Long modifyTime;

}
